package cn.stylefeng.guns.modular.history.service.impl;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.modular.history.entity.VoteHistory;
import cn.stylefeng.guns.modular.history.enums.VoteHistoryExceptionEnum;
import cn.stylefeng.guns.modular.history.mapper.VoteHistoryMapper;
import cn.stylefeng.guns.modular.history.pojo.request.VoteHistoryRequest;
import cn.stylefeng.guns.modular.history.service.VoteHistoryService;
import cn.stylefeng.guns.modular.vote.entity.Works;
import cn.stylefeng.guns.modular.vote.mapper.WorksMapper;
import cn.stylefeng.roses.kernel.auth.api.context.LoginContext;
import cn.stylefeng.roses.kernel.db.api.factory.PageFactory;
import cn.stylefeng.roses.kernel.db.api.factory.PageResultFactory;
import cn.stylefeng.roses.kernel.db.api.pojo.page.PageResult;
import cn.stylefeng.roses.kernel.rule.exception.base.ServiceException;

/**
 * 投票记录表;业务实现层
 *
 * @author songq
 * @date 2025/04/22 14:55
 */
@Service
public class VoteHistoryServiceImpl extends ServiceImpl<VoteHistoryMapper, VoteHistory> implements VoteHistoryService {

	@Resource
	WorksMapper worksMapper; 
	@Resource
	VoteHistoryMapper voteHistoryMapper; 
	@Override
    public void add(VoteHistoryRequest voteHistoryRequest) {
        VoteHistory voteHistory = new VoteHistory();
        BeanUtil.copyProperties(voteHistoryRequest, voteHistory);
        voteHistory.setCreatedTime(new Date());
        voteHistory.setCreatedBy(LoginContext.me().getLoginUser().getAccount());
        this.save(voteHistory);
    }
	
	@Override
	@Transactional
	public synchronized void vote(VoteHistoryRequest voteHistoryRequest,List<Works> works) {
		// TODO Auto-generated method stub
        for (Works w : works) {
        	VoteHistory voteHistory = new VoteHistory();
            BeanUtil.copyProperties(voteHistoryRequest, voteHistory);
            voteHistory.setCreatedTime(new Date());
            voteHistory.setWorkId(w.getWorkId());
            voteHistoryMapper.insert(voteHistory);
    		w.setVoteNum(w.getVoteNum() == null ? 1: w.getVoteNum() + 1);
    		worksMapper.updateById(w);
        }
	}

    @Override
    public void del(VoteHistoryRequest voteHistoryRequest) {
        VoteHistory voteHistory = this.queryVoteHistory(voteHistoryRequest);
        this.removeById(voteHistory.getVoteId());
    }

    @Override
    public void edit(VoteHistoryRequest voteHistoryRequest) {
        VoteHistory voteHistory = this.queryVoteHistory(voteHistoryRequest);
        BeanUtil.copyProperties(voteHistoryRequest, voteHistory);
        this.updateById(voteHistory);
    }

    @Override
    public VoteHistory detail(VoteHistoryRequest voteHistoryRequest) {
        return this.queryVoteHistory(voteHistoryRequest);
    }

    @Override
    public PageResult<VoteHistory> findPage(VoteHistoryRequest voteHistoryRequest) {
        LambdaQueryWrapper<VoteHistory> wrapper = createWrapper(voteHistoryRequest);
        Page<VoteHistory> voteHistory = this.page(PageFactory.defaultPage(), wrapper);
        return PageResultFactory.createPageResult(voteHistory);
    }

    @Override
    public List<VoteHistory> findList(VoteHistoryRequest voteHistoryRequest) {
        LambdaQueryWrapper<VoteHistory> wrapper = this.createWrapper(voteHistoryRequest);
        return this.list(wrapper);
    }

    /**
     * 获取信息
     *
     * @author songq
     * @date 2025/04/22 14:55
     */
    private VoteHistory queryVoteHistory(VoteHistoryRequest voteHistoryRequest) {
        VoteHistory voteHistory = this.getById(voteHistoryRequest.getVoteId());
        if (ObjectUtil.isEmpty(voteHistory)) {
            throw new ServiceException(VoteHistoryExceptionEnum.VOTE_HISTORY_NOT_EXISTED);
        }
        return voteHistory;
    }

    /**
     * 创建查询wrapper
     *
     * @author songq
     * @date 2025/04/22 14:55
     */
    private LambdaQueryWrapper<VoteHistory> createWrapper(VoteHistoryRequest voteHistoryRequest) {
        LambdaQueryWrapper<VoteHistory> queryWrapper = new LambdaQueryWrapper<>();

        Long voteId = voteHistoryRequest.getVoteId();
        Long workId = voteHistoryRequest.getWorkId();
        String voteIp = voteHistoryRequest.getVoteIp();
        String voteClient = voteHistoryRequest.getVoteClient();
        String createdBy = voteHistoryRequest.getCreatedBy();
        String createdTime = voteHistoryRequest.getCreatedTime();
        String updatedBy = voteHistoryRequest.getUpdatedBy();
        String updatedTime = voteHistoryRequest.getUpdatedTime();

        queryWrapper.eq(ObjectUtil.isNotNull(voteId), VoteHistory::getVoteId, voteId);
        queryWrapper.eq(ObjectUtil.isNotNull(workId), VoteHistory::getWorkId, workId);
        queryWrapper.like(ObjectUtil.isNotEmpty(voteIp), VoteHistory::getVoteIp, voteIp);
        queryWrapper.like(ObjectUtil.isNotEmpty(voteClient), VoteHistory::getVoteClient, voteClient);
        queryWrapper.like(ObjectUtil.isNotEmpty(createdBy), VoteHistory::getCreatedBy, createdBy);
        queryWrapper.eq(ObjectUtil.isNotNull(createdTime), VoteHistory::getCreatedTime, createdTime);
        queryWrapper.like(ObjectUtil.isNotEmpty(updatedBy), VoteHistory::getUpdatedBy, updatedBy);
        queryWrapper.eq(ObjectUtil.isNotNull(updatedTime), VoteHistory::getUpdatedTime, updatedTime);

        return queryWrapper;
    }

}